package ch.fusun.baron.treasury.ui;

import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Composite;
import org.eclipse.swt.widgets.Label;
import org.eclipse.ui.part.ViewPart;

import ch.fusun.baron.core.injection.Inject;
import ch.fusun.baron.core.injection.ReInjector;
import ch.fusun.baron.core.service.UserService;
import ch.fusun.baron.data.DataListener;
import ch.fusun.baron.data.DataUpdate;
import ch.fusun.baron.player.api.PlayerService;
import ch.fusun.baron.treasury.api.TreasuryService;

/**
 * Displays the money of the player
 */
public class TreasuryView extends ViewPart implements DataListener {

	private Label moneyLabel;
	@Inject
	private UserService userService;
	@Inject
	private PlayerService playerService;
	@Inject
	private TreasuryService treasuryService;

	/**
	 * Constructor
	 */
	public TreasuryView() {
		ReInjector.getInstance().reInject(this);
	}

	@Override
	public void createPartControl(Composite parent) {
		moneyLabel = new Label(parent, SWT.NONE);
		moneyLabel.setText(new Integer(getMoneyForCurrentPlayer()).toString());
		treasuryService.addDataListener(this);
	}

	private int getMoneyForCurrentPlayer() {
		return treasuryService.getMoney(playerService.getDynasty(userService
				.getUser()));
	}

	@Override
	public void setFocus() {
		// Do nothing
	}

	@Override
	public void dataChanged(DataUpdate update) {
		getSite().getShell().getDisplay().asyncExec(new Runnable() {
			@Override
			public void run() {
				moneyLabel.setText(new Integer(getMoneyForCurrentPlayer())
						.toString());
			}
		});
	}
}
